package com.proposition.paper.service.impl;

import com.proposition.commons.constant.ExamCategory;
import com.proposition.commons.enums.PaperPattern;
import com.proposition.commons.model.StrategyOne;
import com.proposition.paper.service.AbstractPaperAddStrategy;
import com.proposition.paper.service.PaperAddStrategy;
import com.proposition.paper.service.PaperStrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  第一站问答组卷验证
 *
 * @author 杨凯
 * @create 2018-02-02 14:17
 **/
@Slf4j
@Service("wenda")
public class WendaServiceImpl extends AbstractPaperAddStrategy implements PaperAddStrategy {


	@Autowired
	private PaperStrategyService paperStrategyService;


	@Override
	public int firstQuestion(String exam) {
		//获取组卷策略
		StrategyOne one = paperStrategyService.getStrategyOne();
		String examCategory = exam.substring(0, 2);
		if (!one.getExam1().equals(examCategory)) {
			//未按照组卷策略添加
			return 1001;
		}
		boolean flag = checkExamIsUse(PaperPattern.WEN_DA, 1, exam);
		if (flag) {
			//试题已在别的试卷中存在
			return 1002;
		}
		return 200;
	}

	@Override
	public int secondQuestion(String exam) {
		//获取组卷策略
		StrategyOne one = paperStrategyService.getStrategyOne();
		String examCategory = exam.substring(0, 2);
		int count;
		int categoryCount = selectUsedByPaperId(PaperPattern.WEN_DA, 2, examCategory);
		boolean flag = checkExamIsUse(PaperPattern.WEN_DA, 2, exam);
		if (flag) {
			//试题已在别的试卷中存在
			return 1007;
		}
		switch (examCategory) {
			//儿科
			case ExamCategory.ER_KE_WD:
				//未按照组卷策略添加
				count = one.getExam2EK();
				if (count == 0) {
					return 1003;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam2EK()) {
					return 1004;
				}
				break;
			//伤科
			case ExamCategory.SHANG_KE_WD:
				count = one.getExam2SK();
				if (count == 0) {
					return 1003;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam2SK()) {
					return 1004;
				}
				break;
			//妇科
			case ExamCategory.FU_KE_WD:
				count = one.getExam2FK();
				if (count == 0) {
					return 1003;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam2FK()) {
					return 1004;
				}
				break;
			//内科
			case ExamCategory.NEI_KE_WD:
				count = one.getExam2NK();
				if (count == 0) {
					return 1003;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam2NK()) {
					return 1004;
				}
				break;
			//触诊
			case ExamCategory.CHU_ZHEN_WD:
				count = one.getExam2CZ();
				if (count == 0) {
					return 1003;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam2CZ()) {
					return 1004;
				}
				break;
			default:
				//未按照组卷策略添加
				return 1003;
		}
		return 200;
	}

	@Override
	public int thirdQuestion(String exam) {
		//获取组卷策略
		StrategyOne one = paperStrategyService.getStrategyOne();
		String examCategory = exam.substring(0, 2);
		boolean flag = checkExamIsUse(PaperPattern.WEN_DA, 3, exam);
		if (flag) {
			return 1008;
		}
		int count;
		int categoryCount = selectUsedByPaperId(PaperPattern.WEN_DA, 3, examCategory);
		switch (examCategory) {
			//儿科
			case ExamCategory.ER_KE_WD:
				//未按照组卷策略添加
				count = one.getExam3EK();
				if (count == 0) {
					return 1005;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam3EK()) {
					return 1006;
				}
				break;
			//伤科
			case ExamCategory.SHANG_KE_WD:
				count = one.getExam3SK();
				if (count == 0) {
					return 1005;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam3SK()) {
					return 1006;
				}
				break;
			//妇科
			case ExamCategory.FU_KE_WD:
				count = one.getExam3FK();
				if (count == 0) {
					return 1005;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam3FK()) {
					return 1006;
				}
				break;
			//内科
			case ExamCategory.NEI_KE_WD:
				count = one.getExam3NK();
				if (count == 0) {
					return 1005;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam3NK()) {
					return 1006;
				}
				break;
			//触诊
			case ExamCategory.CHU_ZHEN_WD:
				count = one.getExam3CZ();
				if (count == 0) {
					return 1005;
				}
				//如果数量相等 ,则该分类不能进行使用
				if (categoryCount >= one.getExam3CZ()) {
					return 1006;
				}
				break;
			default:
				//未按照组卷策略添加
				return 1005;
		}
		return 200;
	}
}
